package sudoku;

public class Validator {

	
	public Validator(){}
	
	public boolean is_invalid_length(String input)
	{
		if(input.length()>1)
			return true;
		else
			return false;
	}
	public boolean is_invalid_range(String input)
	{
		if(input.charAt(0)-'0'>8 || input.charAt(0)-'0'< 0)
			return true;
		else
			return false;
	}
	public boolean contain_nonzero_position(int xPos,int yPos)
	{
		if(xPos!=0 || yPos!=0)
			return true;
		else
			return false;
	}
	
	public boolean is_invalid_value(String input)
	{
		if(input.charAt(0)-'0' > 9 || input.charAt(0)-'0' < 1 )
			return true;
		else
			return false;
	}
	
	public boolean check_input(char[][] Ques_arr,char [][] Bd_arr, int x, int y, String input, char i, int display)
	{
		int int_input = Integer.valueOf(input).intValue();
		
		if (( x<0 || x>8)||( y<0 || y>8) ||( int_input<1 || int_input>9))//Range Validation
		{
			System.out.print("[Out of range]");
			return false;
		}

		if(Ques_arr[y][x]!=' ')  //if it change default number
		{
			System.out.print("[Question]");
			return false;
		}		
		
		for (int k=0; k<9; k++)  // if it repeat in column or row
		{	
			if(Bd_arr[y][k] == i )
			{
				if(display!=0)
				System.out.print("[Repeated]");
				return false;
			}			
			if(Bd_arr[k][x] == i )
			{
				if(display!=0)
					System.out.print("[Repeated]");
				return false;
				
			}
		}
		
		/*Cheek is it repeat on 3*3
		First set ly and lx represent the top left position on corresponding 3*3
		For example, user input 5 1 5 , the top left position should be column4 row1
		*/
	
		int ly=0; 
		int lx=0; 
		
		switch (y%3) 
		{
			case 0: ly=y; break;
			case 1: ly=y-1;	break;	
			case 2: ly=y-2;	break;
		}
		
		switch (x%3)
		{
			case 0: lx=x; break;
			case 1: lx=x-1;	break;
			case 2: lx=x-2;	break;
		}
		
		//From the top left position start,check the corresponding 3*3 area(Left to right and top to bottom)
		for(int l=ly;l<ly+3;l++) 
		{
			for(int k=lx;k<lx+3;k++)
			{
				if(Bd_arr[l][k]==i)
				{
					if(display!=0)
						System.out.print("[Repeated]");
					return false;
					
				}
			}
		}
		
		return true;
	}
	
	
}
